#! /usr/bin/perl -w
use strict;

# =====================================================================================
#
#       Filename:  GenerateBrowseList
#
#    Description:  Find Entrance Program and Menu for each progress source code.
#           
#          Input:  1.The path of qadadm 
#                   eg. ./GenerateBrowseList $ROOT/database/progress/xml_data/qadadm
#                  2.Empty with the qadadm xmls in the same folder
#                   eg. 
#                       ls ----> brw_mstr-brwt_det-brwf_det-proc_det-prol_det.xml
#                           |--> vue_mstr-vwj_det-vuf_det.xml
#                       ./GenrateBrowseList
#         
#         Output: browse.lst
#
#        Version:  1.0
#        Created:  2011/12/27 09:45:26
#       Revision:  1
#
#         Author:  Wade Xu (jxx), jxx@qad.com
#                   Copyright 1986-2011 QAD Inc., Santa Barbara, CA, USA. 
#
# =====================================================================================

sub GenerateList;
sub PrintListToFile;

# Global Variable Definitions
our @brFile;
our %hashBrowse;
our %hashView;
our @nbrList = ("trnbr", "ieh_trans_id", "kbtr_trans_nbr","kbtrd_kbtr_trans_nbr","knbcd_kbtr_trans_nbr"); 
our @fileList;

# Main Routine
if ( @ARGV == 0 )
{
    $ARGV[0] = "./";
}
$fileList[0] = $ARGV[0]."brw_mstr-brwt_det-brwf_det-proc_det-prol_det.xml";
$fileList[1] = $ARGV[0]."vue_mstr-vwj_det-vuf_det.xml";

my $fileName;
foreach $fileName(@fileList)
{
    open (SOURCEFILE, $fileName) || die "Cannot open file $fileName.";
    @brFile = <SOURCEFILE>;

    print "\nBegin $fileName......\n";
    
    my $nbrField;
    foreach $nbrField(@nbrList)
    {

        my @grepResult = {};
        @grepResult = `grep "$nbrField" $fileName -i -n`;

        my $grepLine;
        foreach $grepLine(@grepResult)
        {
            &GenerateList($grepLine,$fileName);
        }
    }
    close SOURCEFILE;
}

&PrintListToFile;
close SOURCEFILE;

sub GenerateList
{
    my $line = $_[0];
    my $isBrowse = 0;
    if ( $_[1] =~ /brw/ )
    {
        $isBrowse = 1;
    }
    else
    {
        $isBrowse = 0;
    }

    chomp($line);
    if ( $line =~ /(\d+): +(.*)/ )
    {
        my $lineNbr = $1;
        my $lineInfo = $2;
        while ( $brFile[$lineNbr] !~ /name>/ or ($brFile[$lineNbr] =~ /prol_proc_name>/ and $isBrowse == 1 ))
        {
            $lineNbr = $lineNbr - 1;
        }
        
        if ( $brFile[$lineNbr] =~ /name>(.*)</ )
        {
            my $keyName = $1;
            if ( $isBrowse == 1 )
            {
                if ( $lineInfo =~ /<(.*)>(.*)<\/.*>/ )
                {
                    my $type = $1;
                    my $info = $2;
                    if ( $type =~ /brwf_field/ )
                    {
                        $lineNbr = $lineNbr + 4;
                        if ( $brFile[$lineNbr] =~ /<brwf_format>(.*)</ )
                        {
                            if ( exists($hashBrowse{$keyName}) )
                            {
                                $hashBrowse{$keyName} .= "\n\t\t\t";
                            }
                            $hashBrowse{$keyName} .= "$type\t$info format:$1";
                        }
                    }
                    else
                    {
                        if ( exists($hashBrowse{$keyName}) )
                        {
                            $hashBrowse{$keyName} .= "\n\t\t\t";
                        }
                        $hashBrowse{$keyName} .= "$type\t$info";
                    }
                }
            }
            else
            {
                if ( $lineInfo =~ /<(.*)>(.*)<\/.*>/ )
                {
                    my $type = $1;
                    my $info = $2;
                    if ( $type =~ /vuf_field/ )
                    {
                        $lineNbr = $lineNbr + 3;
                        if ( $brFile[$lineNbr] =~ /<vuf_format>(.*)</ )
                        {
                            if ( exists($hashView{$1}) )
                            {
                                $hashView{$1} .= "\n\t\t\t\t";
                            }
                            $hashView{$keyName} .= "$type\t$info format:$1";
                        }
                    }
                    else
                    {
                        if ( exists($hashView{$1}) )
                        {
                            $hashView{$1} .= "\n\t\t\t\t";
                        }
                        $hashView{$keyName} .= "$1\t$2";
                    }
                }
            }
        }
    }
}

sub PrintListToFile
{
    open (OUTFILE, ">browse.lst");
    print OUTFILE ("brw_mstr-brwt_det-brwf_det-proc_det-prol_det.xml :\n");
    print OUTFILE ("Browse Name\t     Type\t              Info\n");
    print OUTFILE ("----------  ----------  ------------------------------------------------------\n");
    
    my @keys = sort keys %hashBrowse;
    foreach my $key (@keys)
    {
        printf OUTFILE ("%10s\t%s\n",$key,$hashBrowse{$key});
    }

    print OUTFILE ("\n\nvue_mstr-vwj_det-vuf_det.xml:\n");
    print OUTFILE ("View Name\t   Browse\t     Type\t                Info\n");
    print OUTFILE ("---------------\t-----\t----------\t------------------------------------------------------\n");
    
    @keys = sort keys %hashView;
    foreach my $key (@keys)
    {
        my $browseName = "";
        $browseName = &findBrowseNameWithView($key);
        printf OUTFILE ("%15s\t%s\t%s\n",$key,$browseName, $hashView{$key});
    }
    close OUTFILE;
}

sub findBrowseNameWithView()
{
    my $viewName = $_[0];
    my @grepResult = `grep "<brw_view>$viewName<" $fileList[0]  -B 3 | grep "<brw_name>"`;
    my $returnName = "";
    foreach my $line(@grepResult)
    {
        if ( $line =~ /<brw_name>(.*)<\// )
        {
            $returnName .= "$1 ";
        }
    }
    return $returnName;
}
